package com.hmbe.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmbe.dto.HmBeUserDto;
import com.hmbe.dto.Result;
import com.hmbe.dto.StatusCode;
import com.hmbe.entity.HmBeUser;
import com.hmbe.entity.HmBeUserInfo;
import com.hmbe.mapper.HmBeUserInfoMapper;
import com.hmbe.mapper.HmBeUserMapper;
import com.hmbe.service.HmBeUserService;
import com.hmbe.utils.GetUserUtils;
import com.hmbe.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

import static com.hmbe.dto.Prompt.*;
import static com.hmbe.utils.RedisConstants.LOGIN_USER_KEY;
import static com.hmbe.utils.RedisConstants.LOGIN_USER_TTL;

@Service
public class HmBeUserServiceImpl extends ServiceImpl<HmBeUserMapper, HmBeUser> implements HmBeUserService {
    @Autowired
    private HmBeUserMapper hmBeUserMapper;
    @Autowired
    private HmBeUserInfoMapper hmBeUserInfoMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Result reg(HmBeUserDto dto) {
        //1.判断用户名是否重复
        HmBeUser isExist = query().eq("username", dto.getUsername()).one();
        if (isExist != null) {
            return Result.fail(FAIL_REG, StatusCode.FAILURE);
        }
        //2.保存注册信息
        int isInsert = saveHmBeUser(dto);
        if (isInsert <= 0) {
            return Result.fail(FAIL_COMMON);
        }
        return Result.ok(SUCCESS_REG, StatusCode.SUCCESS);
    }

    @Override
    public Result login(HmBeUserDto dto) {
        //1.判断用户名和密码是否正确
        HmBeUser hmBeUser = query().eq("username", dto.getUsername()).eq("password", dto.getPassword()).one();
        if (hmBeUser == null) {
            return Result.fail(FAIL_LOGIN, StatusCode.FAILURE);
        }
        //2.登录成功返回token
        String token = TokenUtil.getToken(String.valueOf(hmBeUser.getId()), hmBeUser.getUsername());
        //2.1 将token存入redis
        redisTemplate.opsForValue().set(LOGIN_USER_KEY + hmBeUser.getId(), token, LOGIN_USER_TTL, TimeUnit.MINUTES);
        //3.返回登录结果
        return Result.ok(SUCCESS_LOGIN, StatusCode.SUCCESS, token);
    }

    @Override
    public Result getUserInfo(HttpServletRequest request) {
        Long userId = GetUserUtils.getUserId(request);
        //3.根据用户id查询用户信息
        HmBeUserInfo hmBeUserInfo = hmBeUserInfoMapper.selectById(userId);
        //4.返回用户信息
        return Result.ok(SUCCESS_GET_USER_INFO, StatusCode.SUCCESS, hmBeUserInfo);
    }

    @Override
    public Result updateUserInfo(HmBeUserInfo userInfo, HttpServletRequest httpServletRequest) {
        //1.根据用户id查询用户信息
        HmBeUserInfo hmBeUserInfo = hmBeUserInfoMapper.selectById(userInfo.getUserId());
        //2.更新用户信息
        hmBeUserInfoMapper.updateById(userInfo);
        //3.返回更新结果
        return Result.ok("更新用户信息成功！", StatusCode.SUCCESS);
    }

    private String getUserId(String token) {
        //token字符串中取出id
        String[] split = token.split("id");
        return split[1];
    }

    private int saveHmBeUser(HmBeUserDto dto) {
        HmBeUser hmBeUser = new HmBeUser();
        hmBeUser.setUsername(dto.getUsername());
        hmBeUser.setPassword(dto.getPassword());
        hmBeUser.setRepassword(dto.getRepassword());
        return hmBeUserMapper.insert(hmBeUser);
    }
}
